<template>
    <el-dialog title="更 新" :visible.sync="udpateShow" :before-close="handleClose">
        <el-form ref="dictionaryFrom" :model="dictionaryFrom" label-width="135px" :rules="rules">
        <el-form-item label="字典项" prop="dictionaryKey">
            <el-input v-model="dictionaryFrom.dictionaryKey" disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="字典值"  prop="dictionaryValue">
            <el-input  v-model="dictionaryFrom.dictionaryValue" ></el-input>
        </el-form-item>
         <el-form-item label="备注"  prop="remark">
             <el-input v-model="dictionaryFrom.remark"></el-input>
        </el-form-item>
        <el-form-item label="字典类型分组"  prop="type">
            <el-input v-model="dictionaryFrom.type"></el-input>
        </el-form-item>
        <el-form-item label="是否可编辑"  prop="editable">
             <el-radio-group v-model="dictionaryFrom.editable" >
                <el-radio border label="1" >可编辑</el-radio>
                <el-radio border label="0" >不可编辑</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit('dictionaryFrom')">更 新</el-button>
            <el-button @click="uChange">取 消</el-button>
        </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import { UpdateDictionary,GetDictionary } from '@api/dictionary/dictionary'
  export default {
    data() {
      return {
          udpateShow: this.uShow,
          updateId:this.uId,
        dictionaryFrom: {
          id: '',
          dictionaryKey: '',
          dictionaryValue: '',
          remark:'',
          type: '',
          editable: ''
        },
        rules: {
          dictionaryKey: [
                { required: true, message: '请输入字典项', trigger: 'blur' },
            ],
            dictionaryValue: [
                { required: true, message: '请输入字典值', trigger: 'blur' },
            ]
        }
      }
    },
    /**双向绑定 start */
     props: ["uShow","uId"],
    watch: {
        uShow(val) {
            this.udpateShow = val;//②监听外部对props属性uShow的变更，并同步到组件内的data属性udpateShow中
        },
        udpateShow(val){
            //xxcanghai 小小沧海 博客园
             this.$emit("on-uShow-change",val);//③组件内对udpateShow变更后向外部发送事件通知
        }
        ,
         uId(val) {
            this.updateId = val;//②监听外部对props属性uShow的变更，并同步到组件内的data属性udpateShow中
            this.loadDictionary(val)
        },
        updateId(val){
            //xxcanghai 小小沧海 博客园
             this.$emit("on-id-change",val);//③组件内对udpateShow变更后向外部发送事件通知
        }
    },
  
    methods: {
        uChange() {
            this.udpateShow = !this.udpateShow;
        },
          /**双向绑定 end */

        onSubmit(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                this.loading = true
                UpdateDictionary(this.dictionaryFrom).then(res=>{
                    this.$message({
                    message: '更新成功',
                    type: 'success'
                    });
                     this.$emit("on-loadlist-change");//③组件内对myResult变更后向外部发送事件通知
                     this.loading = false
                     this.uChange()
                }).catch(err=>{
                    console.log('err', err)
                    this.loading = false
                })
               
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        handleClose(done){
            this.$confirm('确定关闭吗').then(() => {
                // function(done)，done 用于关闭 Dialog
                done();
            }).catch(() => {
               // console.log("点击确定时触发");
            });
        },
        loadDictionary(id){
            this.loading = true
            GetDictionary({
                dictionaryId:id+""
            }).then(res=>{
                this.dictionaryFrom = res.data
                this.dictionaryFrom.editable = res.data.editable+''
                this.loading = false
            }).catch(err=>{
                console.log('err', err)
                this.loading = false
            })
        }
    },
    created(){
        //console.log("created");
    },
    mounted(){
        //console.log("mounted");
    },
    computed:{
       
    }
  }
</script>